package com.situ.scientific.dao.impl;

import com.situ.scientific.dao.IAuthDao;
import com.situ.scientific.pojo.Auth;
import com.situ.scientific.pojo.query.AuthQuery;
import com.situ.scientific.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class AuthDaoImpl implements IAuthDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Auth> selectByPage(AuthQuery authQuery) {
        List<Object> queryList = new ArrayList<>();

        String sql = "SELECT id,name,role,status,deleted FROM auth WHERE 1=1";
        String queryName = authQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryRole = authQuery.getRole();
        if (queryRole != null && !"".equals(queryRole)) {
            sql += " AND type_id=?";
            queryList.add(queryRole);
        }

        sql+=" LIMIT ?,?";

        int offset = (authQuery.getPage()-1)*authQuery.getLimit();
        queryList.add(offset);
        queryList.add(authQuery.getLimit());

        return template.query(sql,new BeanPropertyRowMapper<>(Auth.class),queryList.toArray());
    }

    @Override
    public int totalCount(AuthQuery authQuery) {
        List<Object> queryList = new ArrayList<>();
        String queryName = authQuery.getName();
        String sql = "SELECT count(*) FROM auth WHERE 1=1";
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryRole = authQuery.getRole();
        if (queryRole != null && !"".equals(queryRole)) {
            sql += " AND type_id=?";
            queryList.add(queryRole);
        }

        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void add(Auth auth) {
        System.out.println("AuthDaoImpl.add");
        System.out.println(auth);

        String sql = "INSERT INTO auth(name,role,status) VALUES (?,?,?)";
        template.update(sql,auth.getName(),auth.getRole(),auth.getStatus());


    }
    public int deleteById(int id) {
        System.out.println("删除函数内传过来的id是："+id);

        int count = 0;

        String sql = "DELETE FROM auth WHERE id=?";
        count = template.update(sql,id);

        System.out.println("count:"+count);
        return count;
    }

    @Override
    public Auth selectById(String id) {
        System.out.println("AuthDaoImpl.selectById");
        String sql = "SELECT id,name,path,role,status,deleted FROM auth WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Auth.class),id);
    }

    @Override
    public void updateStatus(String id,int status) {
        //写sql语句
        String sql = "UPDATE auth SET status=? WHERE id=?";
        template.update(sql,status,id);
    }

    @Override
    public void update(Auth auth) {
        //写sql语句
        String sql = "UPDATE auth SET name=?,role=? WHERE id=?";
        template.update(sql,auth.getName(),auth.getId());
    }

    @Override
    public List<Auth> selectByRole(Integer role) {
        String sql = "SELECT id,name,path,role,status,deleted FROM auth WHERE role=?";
        return template.query(sql, new BeanPropertyRowMapper<>(Auth.class), role);
    }

}
